using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Petra.Gis;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices.WKT.Support;
using System.IO;

namespace Petra.Gis.DataServices.WKT
{
  public partial class GeometryFactory
  {
    /***********************************************
     * 28.8.2007
     * 
     * creates MultiPoint
     * from wkt string
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    /// <summary>
    /// multiPointToString takes Point only geometry collection as input
    /// and converts all elements in collection from startIndex to binary
    /// startIndex is first element to process
    /// </summary>
    private static GeometryBase ToGeometryCollection(ref string inputStr, System.Type parentGeomType)
    {
      RegexGeometryParser rgp = new RegexGeometryParser(WKTGeometryType.GeometryCollection);
      RegexGeometry rg = rgp.ParseGeometry(ref inputStr);

      GeometryCollection oGeom = new GeometryCollection();

      string tmpName;
      GeometryBase tmpGeom = null;
      for (int i = 0; i < rg.parser.regexMatch.Groups["geometryname"].Captures.Count; i++)
      {
        tmpGeom = null;
        tmpName = rg.parser.regexMatch.Groups["geometryname"].Captures[i].ToString().Trim();
        if (_getGeometryName(ref tmpName) == WKTGeometryType.Point)
          tmpGeom = ToPoint(rg, 0, WKTGeometryType.GeometryCollection);
        if(_getGeometryName(ref tmpName) == WKTGeometryType.LineString)
          tmpGeom = ToLineString(rg, WKTGeometryType.GeometryCollection);
        if (_getGeometryName(ref tmpName) == WKTGeometryType.Polygon)
          tmpGeom = ToPolygon(rg, WKTGeometryType.GeometryCollection);

        if (tmpGeom == null)
          throw new ArgumentException("invalid invalid invalid ;-)", "ble ble");
        else
          oGeom.Add(tmpGeom);
      }

      return oGeom;
    }







    /***********************************************
     * 28.8.2007
     * 
     * creates wkt string 
     * from MultiPoint
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    /// <summary>
    /// multiPointToString takes Point only geometry collection as input
    /// and converts all elements in collection from startIndex to binary
    /// startIndex is first element to process
    /// </summary>
    private static string GeometryCollectionToString(GeometryBase geometry)
    {
      MultiPoint _mp = (MultiPoint)geometry;

      System.IO.StringWriter mStrWriter = new System.IO.StringWriter();


      Point tmpPoint;
      foreach (GeometryBase g in _mp)
      {
        //cast GeometryBase to LineRing
        tmpPoint = (Point)g;
        mStrWriter.Write(CoordinateListConverter.ToString(tmpPoint.Origin, Dimension.dim2D));
      }
      mStrWriter.Write(")");

      return mStrWriter.ToString();

    }

  }
}
